package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.Before;
import org.junit.Test;

import urbanparks.Job;
import urbanparks.JobDuration;
import urbanparks.JobGrade;
import urbanparks.JobSet;
import urbanparks.User;
import urbanparks.UserSet;
import urbanparks.Volunteer;
/**
 * tests the Job class to ensure method correctness.
 * @author Ben Hadorn
 * @version Winter-2012
 */
public class JobTest {

  Job job_from_file;
  Job new_job_from_gui;
  JobGrade light_grade;
  JobGrade med_grade;
  JobGrade high_grade;
  JobDuration job_duration;
  List<List<String>> user_lists = new ArrayList<List<String>>();
  List<String> description = new ArrayList<String>();
  List<Integer> maxs = new ArrayList<Integer>();
  JobSet job_set;
  
  @Before
  public void initilizeData() {
    setJobGrades();
    description.add("blah");
    description.add("blah blah");
    description.add("yada yada");

    maxs.add(5);
    maxs.add(7);
    maxs.add(3);
    int [] duration = {2012, 4, 12, 2};
    job_duration = new JobDuration(2012, 4, 12, 2);
    for (int i = 0; i < 3; i++) {
     user_lists.add(createUserList(maxs.get(i)));
    }
    job_from_file = new Job("Ben", "Park Maintenance", "Fae Bainbridge", "hadornb@uw.edu", 
                            description, maxs, duration, user_lists);
    
    new_job_from_gui = new Job("Ben", "Clean Stuff", "Cannon Hill", "hadornb@uw.edu", description, maxs, duration);
    job_set = new JobSet(new File("jobs.txt"), 3, 2);
  }

  
  @Test
  public void testAddVolunteer() {
    
    Volunteer me = new Volunteer("Ben", "Hadorn", "hadornb@uw.edu", UserSet.USER_VOLUNTEER, "555-867-5309", false);
    
    int before = getSubListSizes(job_from_file.getVolunteerList());
    assertTrue("user wasn't added",job_from_file.addVolunteer(me, light_grade, job_set));
    int after = getSubListSizes(job_from_file.getVolunteerList());
    assertEquals("derp", before + 1 , after);
  }
  
  
  @Test
  public void testAddVolunteerTwiceToSameJob() {
    
    Volunteer me = new Volunteer("Ben", "Hadorn", "hadornb@uw.edu", UserSet.USER_VOLUNTEER, "555-867-5309", false);
    
    assertTrue("user wasn't added",job_from_file.addVolunteer(me, light_grade, job_set));
    /**
     * Should fail because this user was already added.
     */    
    assertFalse("user wasn't added",job_from_file.addVolunteer(me, light_grade, job_set));
  }
  
  
  @Test
  public void testContainsVolunteer() {
       
    Volunteer me = new Volunteer("Ben", "Hadorn", "hadornb@uw.edu", UserSet.USER_VOLUNTEER, "555-867-5309", false);
    
    assertTrue("user wasn't added",job_from_file.addVolunteer(me, light_grade, job_set));
    assertTrue("user wasn't found", job_from_file.containsVolunteer(me.getEmailAddress()));
  }

  @Test
  public void testToString() {
    assertEquals("not the same output", "Fae Bainbridge", job_from_file.toString());
  }

  @Test
  public void testToParkDateString() {
    String temp = job_from_file.toString() + ", " + job_duration.toString();
    assertEquals("not the same output", temp, job_from_file.toParkDateString());
  }
  
  private List<String> createUserList(final int size){
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    List<String> temp_list = new ArrayList<String>();
    Random make_random = new Random();
    String tmp;
    for (int i = 0; i < size - 1; i++) {
      tmp = new String();
      for (int x = 0; x < make_random.nextInt(alphabet.length()) + 1; x++) {
        tmp  += (alphabet.charAt(make_random.nextInt(alphabet.length())));
      }
     temp_list.add(tmp);
    }
    return temp_list;
  }
  private void setJobGrades() {
    light_grade = new JobGrade("LIGHT", "breathing", 5);
    med_grade = new JobGrade("MED", "walking", 7);
    
  }

  public int getSubListSizes(final List<List<String>> the_master_list) {
    int size = 0;
    for (List<String> a : the_master_list) {
      size += a.size();
    }
    return size;
  }
  
}
